<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Victor
 * Date: 07.12.11
 * Time: 3:28
 * To change this template use File | Settings | File Templates.
 */

class Lesson_Lesson extends Core_BaseModel
{
    public function getAllLessons()
    {
        $result = null;
        $pdo = $this->dataModule->getConnection();
        $sql = "select `idlesson`,`title` from `dzundza_lesson`";
        $stn = $pdo->prepare($sql);
        $stn->execute();
        $result = $stn->fetchAll(PDO::FETCH_ASSOC);
        return $result;
    }

    public function getAllLessonsByCourse($idCourse)
    {
        $result = null;
        $pdo = $this->dataModule->getConnection();
        $sql = "select `idlesson`,`title`,`description` from `dzundza_lesson`
              where `course_idcourse` = :idcourse";
        $stn = $pdo->prepare($sql);
        $stn->bindParam(':idcourse', $idCourse, PDO::PARAM_INT);
        $stn->execute();
        $result = $stn->fetchAll(PDO::FETCH_ASSOC);
        return $result;
    }

    public function addPart($idLesson, $title, $content)
    {
        try {
            $pdo = $this->dataModule->getConnection();
            $pdo->beginTransaction();
            $sql = "INSERT INTO `dzundza_part` (`lesson_idlesson`,`Title`,`content`) VALUES (:idLesson,:title,:content)";
            $stn = $pdo->prepare($sql);
            $stn->bindParam(':idLesson', $idLesson, PDO::PARAM_INT);
            $stn->bindParam(':title', $title, PDO::PARAM_STR);
            $stn->bindParam(':content', $content, PDO::PARAM_STR);
            $stn->execute();
            $pdo->commit();
            $result = $stn->errorCode();
            if ($result == '00000') return true; else return false;
        } catch (PDOException $e) {
            $pdo->rollBack();
            echo "Exception: $e </br>";

        }
    }

    public function addCompletedLesson($lessonId, $userProfile)
    {
        $pdo = null;
        //echo "<pre>addCompletedLesson($lessonId, $userProfile)<br/></pre>";
        try {
            $pdo = $this->dataModule->getConnection();
            $pdo->beginTransaction();
            $sql = "INSERT INTO `dzundza_completedlesson` (`idLesson`, `iduserProfile`)
                VALUES (:idLesson, :iduserProfile)";
            $stn = $pdo->prepare($sql);
            $stn->bindParam(':idLesson', $lessonId, PDO::PARAM_INT);
            $stn->bindParam(':iduserProfile', $userProfile, PDO::PARAM_INT);
            $stn->execute();
            $pdo->commit();
            //var_dump($stn->errorInfo());
        } catch (PDOException $e) {
            $pdo->rollBack();
            $this->vardump($e->errorInfo);
        }

    }

    public function isCompleted($lessonId, $userProfile)
    {
        //echo "isCompleted($lessonId, $userProfile)<br/>";
        try {
            $pdo = $this->dataModule->getConnection();
            $sql = "SELECT count(*) as `count` FROM `dzundza_completedlesson`
where `idLesson` = :lessonId AND
`iduserProfile` = :userProfile";
            $stn = $pdo->prepare($sql);
            $stn->bindParam(':lessonId', $lessonId);
            $stn->bindParam(':userProfile', $userProfile);
            $stn->execute();
            $result = $stn->fetch(PDO::FETCH_ASSOC);
            $count = $result['count'];
            if ($count > 0) return true; else return false;

        } catch (PDOException $e) {
            $this->vardump($e);
        }
    }

    public function setLessonMark($lessonId, $userProfile, $mark)
    {
        $pdo = $this->dataModule->getConnection();
        $sql = "INSERT INTO `dzundza_userlessonmarks`
          (`idProfile`,
            `idLesson`,
              `mark`)
          VALUES
            (
              :userProfile,
              :lessonId,
              :mark
             )";
        $stn = $pdo->prepare($sql);
        $stn->bindParam(':lessonId', $lessonId);
        $stn->bindParam(':userProfile', $userProfile);
        $stn->bindParam(':mark', $mark, PDO::PARAM_INT);
        $stn->execute();
    }

    public function getLessonMark($lessonId, $userProfile)
    {
        $pdo = $this->dataModule->getConnection();
        $sql = "SELECT
`dzundza_userlessonmarks`.`mark`
FROM `dzundza_userlessonmarks`
where `idProfile` = :idProfile AND `idLesson` = :idLesson
";
        $stn = $pdo->prepare($sql);
        $stn->bindParam(':idLesson', $lessonId);
        $stn->bindParam(':idProfile', $userProfile);
        $stn->execute();
        $result = $stn->fetch(PDO::FETCH_COLUMN);
        return $result;
    }

    public function getCompletedLesson($userProfile)
    {
        $pdo = $this->dataModule->getConnection();
        $sql = "SELECT `idLesson` FROM `dzundza_completedlesson`
                where `iduserProfile` = :userProfile";
        $stn = $pdo->prepare($sql);
        $stn->bindParam(':userProfile', $userProfile);
        $stn->execute();
        //var_dump($stn->errorInfo());
        $result = $stn->fetchAll(PDO::FETCH_ASSOC);
        $out = array();
        foreach ($result as $key => $value) {
            $out[] = $value['idLesson'];
        }
        //var_dump($out);
        return $out;


    }

    public function getLessonParts($idLesson)
    {
        //echo "getLessonParts($idLesson)</br>";
        $pdo = $this->dataModule->getConnection();
        $sql = "SELECT `Title`,`content` FROM `dzundza_part`
                where `lesson_idlesson` = :idLesson";
        $stn = $pdo->prepare($sql);
        $stn->bindParam(':idLesson', $idLesson);
        $stn->execute();

        $result = $stn->fetchAll(PDO::FETCH_ASSOC);
        //var_dump($stn->errorInfo());
        return $result;
    }

    public function getUserLessonMarks($profileName)
    {
        try {
            $pdo = $this->dataModule->getConnection();
            $sql = "Select `title`,`mark`
from `dzundza_userlessonmarks`, `dzundza_lesson`
where `dzundza_userlessonmarks`.`idLesson` = `dzundza_lesson`.`idlesson` AND
`idProfile` = :idProfile";

            $auth = new Auth_Auth2();
            $idProfile = $auth->getIdProfile($profileName);
            $stn = $pdo->prepare($sql);
            $stn->bindParam(':idProfile', $idProfile, PDO::PARAM_INT);
            $stn->execute();

            $result = $stn->fetchAll(PDO::FETCH_ASSOC);
            return $result;


        } catch (PDOException $e) {
            $this->vardump($e);


        }

    }

}
